package org.jeecg.modules.system.controller.wyt;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserAgent;
import org.jeecg.modules.system.entity.wyt.TbOrder;
import org.jeecg.modules.system.service.wyt.ITbOrderService;
import org.jeecg.modules.system.vo.SysDictPage;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/visit/ord")
public class TbOrderController {

    @Autowired
    private ITbOrderService orderService;

    /**
     * 根据条件查询所有订单
     * @param order
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET )
    public Result<IPage<TbOrder>> list(TbOrder order,
                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                       HttpServletRequest req){

        //创建查询结果
        Result<IPage<TbOrder>> result = new Result<IPage<TbOrder>>();
        //条件构造器：封装页面传回来的所有查询条件
        QueryWrapper<TbOrder> queryWrapper = QueryGenerator.initQueryWrapper(order, req.getParameterMap());
        //设置分页信息（当前页，页大小）
        Page<TbOrder> page = new Page<TbOrder>(pageNo, pageSize);
        //将查询到的数据进行分页封装
        IPage<TbOrder> orderPage = orderService.page(page,queryWrapper);
        //执行到当前位置时，将result设置为成功
        result.setSuccess(true);
        //将查询到的数据封装到result中
        result.setResult(orderPage);
        //返回查询结果
        return result;

    }

    /**
     * @功能：编辑
     * @param order
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Result<TbOrder> edit(@RequestBody TbOrder order) {
        Result<TbOrder> result = new Result<TbOrder>();
        TbOrder orders = orderService.getById(order.getId());
        if(orders==null) {
            result.error500("未找到对应实体");
        }else {
            orders.setUpdateTime(new Date());
            boolean ok = orderService.updateById(order);
            //TODO 返回false说明什么？
            if(ok) {
                result.success("编辑成功!");
            }
        }
        return result;
    }

    /**
     * 发货/退款
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/shipmentOrRefund", method = RequestMethod.PUT)
    public Result<SysUser> shipmentOrRefund(@RequestBody JSONObject jsonObject) {
        Result<SysUser> result = new Result<SysUser>();
        try {
            String ids = jsonObject.getString("ids");
            String status = jsonObject.getString("status");
            String[] arr = ids.split(",");
            for (String id : arr) {
                if(oConvertUtils.isNotEmpty(id)) {
                    TbOrder ord=  new TbOrder();
                    ord.setOrderStatus(Integer.parseInt(status));
                    this.orderService.update(ord,
                            new UpdateWrapper<TbOrder>().lambda().eq(TbOrder::getId,id));
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败"+e.getMessage());
        }
        result.success("操作成功!");
        return result;
    }


}
